<?php
namespace app\admin\model\myshuju\myconfigs;
use app\admin\model\MyModels;
use think\facade\Db;
use think\Request;

class MyConFig
{
    public $cid = '';
    public $cidinfo = [];
    public $modelinfo =[];
    public $model_id = '';
    public $model = [];
    public $field = [];
    public $table = '';
    public $id = '';
    public $data = [];
    public $dataall = [];
    public $pid = '';

    /**设置cid
     * @param string $cid
     * @return $this
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setCid($cid=''){
        if($cid){
            $this->cid = $cid;
        }
        $this->cidinfo = Db::name(getTable('column'))
            ->where('id',$cid)
            ->find();
        $this->model_id = $this->cidinfo['model_id'];
        $this->model = Db::name(getTable('models'))
            ->where('id',$this->model_id)
            ->find();
        if($this->model){
            $this->table = $this->model['table'];
        }
        $this->field = Db::name(getTable('ziduancid'))
            ->where('cid',$cid)
            ->order('sort desc')
            ->select()->toArray();
        return $this;
    }

    /**设置id
     * @param string $id
     * @return $this
     */
    public function setId($id=''){
        if($id){
            $this->id = $id;
        }
        return $this;
    }

    /**设置pid
     * @param string $pid
     * @return $this
     */
    public function setPid($pid=''){
        $this->pid = $pid;
        return $this;
    }

    /**获取当前数据
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getData($isarray=false){
        if($isarray){
            $this->data =  Db::name($this->table)->where('id','in',$this->id)->select()->toArray();
        }else{
            $this->data = Db::name($this->table)->where('id',$this->id)->find();
        }
        return $this->data;
    }

    /**锅炉建
     * @param $data
     * @param $array
     * @return array
     */
    public function guolu($data,$array){
        $newdata = [];
        foreach ($data as $key=>$value){
            if(!in_array($key,$array)){
                $newdata[$key] = $value;
            }
        }
        return $newdata;
    }

    /**获取所有数据
     * @param array $where
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDataAll($where=[]){
        $this->dataall = Db::name($this->table)
            ->where($where)
            ->select()->toArray();
        return $this->dataall;
    }

    /**获取条件
     * @param Request $request
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getWhere(Request $request){
        $cid = $request->param('cid',null);
        if($cid){
            $this->setCid($cid);
        }
        $model_id = $request->param('model_id');
        if($model_id){
            $this->setModelId($model_id);
        }

        $data = $request->param();
        $where = [];
        foreach ($this->field as $value){
            if($data[$value['field'].'|start']??null){
                if(!$data[$value['field'].'|start']){
                    continue;
                }
                $where[] = [$value['field'],'>=',strtotime($data[$value['field'].'|start'])];
            }
            if($data[$value['field'].'|end']??null){
                if(!$data[$value['field'].'|end']){
                    continue;
                }
                $where[] = [$value['field'],'<=',strtotime($data[$value['field'].'|end'])];
            }
            if(array_key_exists($value['field'],$data)){
                if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'){
                    if(!$data[$value['field']]){
                        continue;
                    }
                    $where[] = [$value['field'],'like',"%".$data[$value['field']]."%"];
                }
                if($value['form_type']=='开关'){
                    if($data[$value['field']]!=0&&!$data[$value['field']]){
                        continue;
                    }
                    if(is_numeric($data[$value['field']])){
                        $where[] = [$value['field'],'=',$data[$value['field']]];
                    }else{
                        if($data[$value['field']]=='开启'){
                            $tiaojian = 1;
                        }else{
                            $tiaojian = 0;
                        }
                        $where[] = [$value['field'],'=',$tiaojian];
                    }
                }
                if($value['form_type']=='联动下拉框'||$value['form_type']=='联动单选框'){
                    if(!$data[$value['field']]){
                        continue;
                    }
                    $where[] = [$value['field'],'=',$data[$value['field']]];
                }
                if($value['form_type']=='联动单行文本'){
                    if(!$data[$value['field']]){
                        continue;
                    }
                    $mytable = Db::name('models')->where('id',$value['table'])->find();
                    if($mytable){
                        $op = 'name';
                        if(!empty($value['option'])){
                            $op = $value['option'];
                        }
                        $array = Db::name($mytable['table'])->where($op,$data[$value['field']])->find();
                        if($array){
                            $where[] = [$value['field'],'=',$array['id']];
                        }
                    }
                }
                if($value['form_type']=='联动下拉组'){
                    if(!$data[$value['field']]){
                        continue;
                    }
                    $table = explode(',',$value['table']);
                    $table = Db::name(getTable('models'))
                        ->where('id',$table[1])
                        ->find();
                    $mydas = Db::name($table['table'])
                        ->where('name','like',"%".$data[$value['field']]."%")
                        ->select();
                    $mydasid = [];
                    foreach ($mydas as $vvv){
                        $mydasid[] = $vvv['id'];
                    }
                    $where[] = [$value['field'],'in',$mydasid];
                }
                if($value['form_type']=='无限联动框'){
                    if(!$data[$value['field']]){
                        continue;
                    }
                    $table = MyModels::getModel($value['table']);
//                                echo $tiaojian;exit;
                    $shs = Db::name($table)
                        ->where('name',"like","%".$data[$value['field']]."%")
                        ->select();
                    $newshs = [];
                    foreach ($shs as $vss){
                        $newshs[] = $vss['id'];
                    }
                    $where[] = [$value['field'],'in',$newshs];
                }
                if($value['form_type']=='联动多选框'){
                    if(!$data[$value['field']]){
                        continue;
                    }
                    $uidtable = Db::name(getTable('models'))
                        ->where('id',$value['table'])
                        ->find();
                    if($uidtable){
                        $array = Db::name($uidtable['table'])
                            ->where('name','like',"%".$data[$value['field']]."%")
                            ->select();
                        $newarray = [];
                        foreach ($array as $vs){
                            $newarray[] = $vs['id'];
                        }
                        $array = Db::name(getTable('foreign_key'))
                            ->where('left_table',$this->table)
                            ->where('right_table',$uidtable['table'])
                            ->where('right_id','in',$newarray)
                            ->field('left_id')
                            ->group('left_id')
                            ->select();
                        $newarray = [];
                        foreach ($array as $vs){
                            $newarray[] = $vs['left_id'];
                        }
                        if($newarray){
                            $where[] = ['id','in',$newarray];
                        }
                    }
                }
                if($value['form_type']=='多选框'){
                    if(!$data[$value['field']]){
                        continue;
                    }
                    $where[] = [$value['field'],'like',"%".$data[$value['field']]."%"];
                }
                if($value['form_type']=='单选框'){
                    if(!$data[$value['field']]){
                        continue;
                    }
                    $where[] = [$value['field'],'like',"%".$data[$value['field']]."%"];
                }
            }
        }
//        echo '<pre>';
//        print_r($where);
//        exit;
        return $where;
    }
    /**设置model_id
     * @param string $model_id
     * @return $this
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setModelId($model_id=''){
        if($model_id){
            $this->model_id = $model_id;
        }
        $this->model = Db::name(getTable('models'))
            ->where('id',$model_id)
            ->find();
        if($this->model){
            $this->table = $this->model['table'];
        }
        $this->field = Db::name(getTable('fields'))
            ->where('model_id',$this->model_id)
            ->order('sort desc')
            ->select()->toArray();
        return $this;
    }

    /**获取数据
     * @param Request $request
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getFormData(Request $request,$callback=''){
        $data = $request->param();
        if($data['cid']??null){
            $this->setCid($data['cid']);
        }
        if($data['model_id']??null){
            $this->setModelId($data['model_id']);
        }
        $field = $this->field;
        $newdata = [];
        if($data['id']??null){
            $newdata['id'] = $data['id'];
        }
        foreach ($field as $value){
            if($value['form_type']=='无限联动框'||$value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='编辑框'||$value['form_type']=='编辑框(layui)'||$value['form_type']=='单图片'||$value['form_type']=='视频'||$value['form_type']=='多图片'||$value['form_type']=='单文件'||$value['form_type']=='多文件'||$value['form_type']=='联动下拉框'||$value['form_type']=='联动下拉组'){
                if(isset($data[$value['field']])&&($data[$value['field']]||$data[$value['field']]==0)){
                    $newdata[$value['field']] = $data[$value['field']];
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='联动单行文本'){
                $mytable = Db::name('models')->where('id',$value['table'])->find();
                if(isset($data[$value['field']])&&($data[$value['field']]||$data[$value['field']]==0)){
                    if($mytable){
                        $array = Db::name($mytable['table'])
                            ->where('name', $data[$value['field']])
                            ->find();
//                        echo '<pre>';
//                        print_r($mytable);
//                        exit;
                        if($array){
                            $newdata[$value['field']] = $array['id'];
                        }else{
                            $newdata[$value['field']] = '';
                        }
                    }else{
                        $newdata[$value['field']] = $data[$value['field']];
                    }
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='时间'){
//                echo $value['field'].':'.$data[$value['field']];
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = strtotime($data[$value['field']]);
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='多选框'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = implode(',',$data[$value['field']]);
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='开关'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = 1;
                }else{
                    $newdata[$value['field']] = 0;
                }
            }
            if($value['form_type']=='单选框'||$value['form_type']=='联动单选框'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = $data[$value['field']];
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='多选框'||$value['form_type']=='联动多选框'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = implode(',',$data[$value['field']]);
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='json数组'){
                $options = explode(':',$value['option']);
                $shujuus = $data[$value['field']];
                $newshuju = [];
                foreach ($shujuus[$options[0]] as $key=>$v){
                    $newshuju[$v] = $shujuus[$options[1]][$key];
                }
                $newdata[$value['field']] = json_encode($newshuju,JSON_UNESCAPED_UNICODE);
            }
            if($callback){
                $newdata[$value['field']] = $callback($value,$data[$value['field']]);
            }
        }
        return $newdata;
    }
}